<template>
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      default-expand-all
      :check-strictly="true"
      node-key="id"
    />
    <div>
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="doSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { array2Tree } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 0.1权限列表
      list: []
    }
  },
  // 1 created
  created() {
    // 1.1获取权限列表
    this.loadPermissionList()
    // 1.2 获取当前点击的权限
    this.loadPermission()
  },
  // 2.方法
  methods: {
    // 2.1关闭按钮
    hCancel() {
      this.$emit('hClose')
    },
    // 2.2获取权限列表
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = array2Tree(res.data, '0')
    },
    // 2.3获取权限点
    async loadPermission() {
      const res = await getRoleDetail(this.curId)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    // 确认
    async  doSubmit() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      await assignPerm({ id: this.curId, permIds: permIds })
      this.$message.success('分配权限成功')
      this.$emit('hClose')
    }
  }
}
</script>

<style>

</style>
